// pages/questionnaire/questionnaire.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content:"",
    id:"",
    typs:"",
    f_id:"",
    j_id:"",
    type:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that  =this;
    var id = options.id;
    that.setData({
      id:id
    })
    var typs = options.typs;
    var type = options.type;
    var f_id = options.f_id;
    var j_id = options.j_id;
    if(type != undefined && type != 'undefined')
    {
      that.setData({
        type:type
      })
    }
    if(typs != undefined && typs != 'undefined')
    {
      that.setData({
        typs:typs,
        f_id:f_id,
        j_id:j_id
      })
    }
    if(typs == 'suifang')
    {
      app.util.request({
        'url':"entry/wxapp/follow.add_visit",
        data:{
          id:id,
          f_id:f_id,
          j_id:j_id,
          openid:wx.getStorageSync('openid'),
          type:1,
        }
      })
    }
    that.getDetail();
  },
  getDetail:function(res){
    var that = this
    app.util.request({
      'url':"entry/wxapp/follow.wenjuan_detail",
      data:{
        id:that.data.id,
        openid:wx.getStorageSync('openid'),
        j_id:that.data.j_id,
        f_id:that.data.f_id,
      },
      success:function(res)
      {
        that.setData({
          info:res.data,
          content:res.data.content
        })
      }
    })
  },
  // 单选
  radioChange(e){
    console.log("单选",e)
    var title = e.detail.value;
    var that = this;
    var index = e.target.dataset.index;
    var title =e.detail.value;
    var content = that.data.content;
    for(var j=0;j<content[index].daans.length;j++)
    {
      console.log(content[index].daans[j].title == title);
      console.log(title);
      if(content[index].daans[j].title == title)
      {
        content[index].daans[j].value = true
        content[index].huida = title
      }
    }
    that.setData({
      content:content
    })
    
  },
  // 多选
  checkboxChange(e){
    var that = this;
    console.log("多选",e)
    var index = e.target.dataset.index;
    var indexs =e.detail.value;
    var content = that.data.content;
    for(var i=0;i<indexs.length;i++)
    {
      content[index].daans[indexs[i]].value = true
      content[index].huida += content[index].daans[indexs[i]].title
    }
    that.setData({
      content:content
    })
    console.log(content)
  },
  changecontent:function(e){
    var that = this;
    var index = e.target.dataset.index;
    var contents = e.detail.value;
    var content = that.data.content;
    console.log(e);
    console.log(content)
    content[index].daans = contents;
    content[index].huida = contents;
    console.log(content);
    that.setData({
      content:content
    })
  },

  // 时间
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  // 提交
  formSubmit(e){
    var that = this;
    var content = that.data.content;
    for(var i=0;i<content.length;i++)
    {
      console.log(content[i].huida)
      if(content[i].huida == '' || content[i].huida == undefined)
      {
        wx.showToast({
          title: '请完善问卷',
        })
        return;
      }

    }
    
    var id = that.data.id;
    var f_id = that.data.f_id;
    var j_id = that.data.j_id;
    app.util.request({
      'url':"entry/wxapp/follow.add_wenjuan",
      data:{
        id:id,
        f_id:f_id,
        j_id:j_id,
        h_id:that.data.info.h_id,
        title:that.data.info.name,
        content:JSON.stringify(content),
        openid:wx.getStorageSync('openid')
      },
      type:"post",
      success:function(res)
      {
        wx.navigateBack({})
      }
    })
  },
  
})